package com.bkjk.platform.halo;

import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;

/**
 * 版权：Copyright 2017 QuarkFinance IT
 * 描述：<描述>
 * 创建人：ShaozeWang
 * 创建时间：2017/11/22
 * 备注:
 */
public class CorsFillter implements Filter {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {


            HttpServletResponse response = (HttpServletResponse) servletResponse;
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            Collection<String> origins = response.getHeaders("Access-Control-Allow-Origin");
            if(null!=origins&&origins.size()>0){

            }else {
                if(!StringUtils.isEmpty(request.getHeader("Origin"))){
                    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
                    response.setHeader("Access-Control-Allow-Credentials", "true");
                    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
                    response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
                    response.setHeader("Access-Control-Allow-Methods", request.getHeader("Access-Control-Request-Method"));
                    response.setHeader("Allow", response.getHeader("Access-Control-Allow-Methods"));

                }}
            // zuul会把option请求转给后端实例，这里做拦截
            if(request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.toString())){
                return;
            }
            chain.doFilter(servletRequest,servletResponse);
        }

        @Override
        public void destroy() {

        }
}
